package com.example.frn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.frn.dao.FilmMapper;
import com.example.frn.dto.FilmInsertDto;
import com.example.frn.dto.FilmUpdateDto;
import com.example.frn.entity.Film;
import com.example.frn.entity.FilmCategory;
import com.example.frn.service.FilmService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@Service
public class FilmServiceImpl implements FilmService {
    @Autowired
    private FilmMapper filmMapper;
    @Transactional //保证save方法下的操作在同一个事务下进行，同时成功或失败
    @Override
    public boolean save(FilmInsertDto dto) {
        Film film = BeanUtil.copyProperties(dto, Film.class);
        filmMapper.insert(film);
        List<FilmCategory> filmCategoryList = new LinkedList<>();
        dto.getCategoryIds().forEach(item ->{
            FilmCategory filmCategory = new FilmCategory(film.getId(),item);
            filmCategoryList.add(filmCategory);
        });
        filmMapper.insertBatchFilmCategory(filmCategoryList);
        film.getMakerList().forEach(item ->{
            item.setFilmId(film.getId());
        });
        filmMapper.insertBatchFilmMaker(dto.getMakerList());
        return true;
    }
    @Transactional
    @Override
    public boolean updateById(FilmUpdateDto dto) {
        Film film = BeanUtil.copyProperties(dto, Film.class);
        filmMapper.updateById(film);
        //更新电影类别
        filmMapper.deleteFilmCategory(film.getId());
        //更新 影人
        filmMapper.deleteFilmMaker(film.getId());
        List<FilmCategory> filmCategoryList = new LinkedList<>();
        dto.getCategoryIds().forEach(item ->{
            FilmCategory filmCategory = new FilmCategory(film.getId(),item);
            filmCategoryList.add(filmCategory);
        });
        filmMapper.insertBatchFilmCategory(filmCategoryList);
        film.getMakerList().forEach(item ->{
            item.setFilmId(film.getId());
        });
        filmMapper.insertBatchFilmMaker(film.getMakerList());
        return true;
    }

    @Override
    public boolean removeById(Integer id) {
        return filmMapper.deleteById(id)>0;
    }

    @Override
    public Film getById(Integer id) {
        Film film = filmMapper.selectById(id);
        List<HashMap<String,Object>> scoreStatistics = filmMapper.selectScoreStatistics(id);
        int total = 0;
        for (HashMap<String, Object> item : scoreStatistics) {
            int star = Integer.valueOf(item.get("star").toString());
            int count = Integer.valueOf(item.get("count").toString());
            total+= count;
            film.getStatistics()[star] = count;
        }
        film.getStatistics()[0] =total;
        return film;
    }

    @Override
    public List<Film> list(String name, Integer cid) {
        return filmMapper.list(name,cid);
    }

    @Override
    public PageInfo<Film> page(Integer pageNum, Integer pageSize, String name, Integer cid) {
        PageHelper.startPage(pageNum,pageSize);
        List<Film> list = this.list(name,cid);
        return new PageInfo<>(list);
    }
}
